package com.lzx.lizhenxing.pay.controller;


import com.lzx.lizhenxing.common.annotation.NoLogin;
import com.lzx.lizhenxing.common.constrants.CommonConstants;
import com.lzx.lizhenxing.common.vo.CommonResult;
import com.lzx.lizhenxing.pay.param.TradePayParam;
import com.lzx.lizhenxing.pay.service.TradeOrderPayService;
import com.lzx.lizhenxing.pay.vo.PayQrVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/web/order")
public class TradeOrderPayController {

    @Resource
    private TradeOrderPayService tradeOrderPayService;

    @PostMapping("/pay")
    public CommonResult tradeOrderPay(@RequestBody TradePayParam tradePayParam) {
        PayQrVO vo = tradeOrderPayService.tradeOrderPay(tradePayParam);
        return CommonResult.success(vo);
    }

    @NoLogin
    @PostMapping("/notify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) {
        tradeOrderPayService.payNotify(request, response);
    }

    @GetMapping("/pay/states")
    public CommonResult tradePayStates(@RequestParam Long orderId) {
        try {

            return CommonResult.success(tradeOrderPayService.payStates(orderId));
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error(CommonConstants.ERROR,e.getMessage());
        }
    }

    @GetMapping("/pay/cancel")
    public CommonResult tradePayCancel(@RequestParam Long orderId) {
        try {

            return CommonResult.success(tradeOrderPayService.tradePayCancel(orderId));
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error(CommonConstants.ERROR,e.getMessage());
        }
    }

}
